//
//  JXTabBar.swift
//  swift高仿微博
//
//  Created by peijiaxing on 16/2/24.
//  Copyright © 2016年 peijiaxing. All rights reserved.
//

import UIKit

class JXTabBar: UITabBar {

    // 1 重写init
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    // 2 创建一个私有（private）方法 添加视图  设置约束
    private func setupUI(){
        
        addSubview(composeButton)
    }
    
    // 3 懒加载
    lazy var composeButton:UIButton = {
        // 创建一个按钮
        let button = UIButton(type: UIButtonType.Custom)
        // 设置属性
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.sizeToFit()
        return button
    }()
    
    //布局
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        // 创建 控件的大小
        var index = 0
        let width = bounds.width / 5
        let height = bounds.height
        
        // 遍历控件  布局
        
        for subview in subviews {
            
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
            subview.frame = CGRectMake(CGFloat (index++) * width, 0, width , height)
                if index == 2 {index++}
            
            }
            
        }
        composeButton.frame = CGRectMake(width * 2, 0, width, height)
    }
}
